package com.bmp.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.bmp.dao.UserDao;
import com.bmp.model.User;
import com.bmp.model.vo.UserVoGroups;
import com.bmp.util.HibernateUtil;

@Component("userDao")
public class UserDaoImpl implements UserDao {
	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public boolean checkUserExistsWithLoginInfo(String username, String password) {
		List<User> users = hibernateTemplate
				.find("from User u where u.username='" + username
						+ "' and u.password='" + password + "'");
		int count = users.size();
		if (count > 0){
			return true;
		}
		else
			return false;
	}
	
	public void deleteUserById(int id) {
		User u = this.getUserById(id);
		this.hibernateTemplate.delete(u);
	}

	public User getUserById(int id) {
		List<User> user = hibernateTemplate.find("from User u where u.id='"+id+"'");
		if(user.isEmpty())  return null;
		else
		return user.get(0);
	}

	public void insert(User u) {
		this.hibernateTemplate.save(u);
	}

	public void updateUser(User u) {
		this.hibernateTemplate.update(u);
	}

	public User getUserByNameAndPassword(String username, String password) {
		List<User> user = hibernateTemplate.find("from User u where u.username='" + username
						+ "' and u.password='" + password + "'");
		if(user.isEmpty()) return null;
		else return user.get(0);
	}

	public List<User> getAllUsers() {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		String sql = "from User u";
		List<User> users  = session.createQuery(sql).list();
		//List<User> users = hibernateTemplate.find("from User u");	
		return users;
	}

	//获取总记录数
	public int getTotalPages() {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		String sql = "select count(*) from User u";
		long count = (Long)session.createQuery(sql).uniqueResult();
		session.getTransaction().commit();
		//System.out.println(count);
		return (int)count;
	}
	//获取每一页的用户
	@SuppressWarnings("unchecked")
	public List<UserVoGroups> getUser(int page, int rows) {
		List<UserVoGroups> list = null;
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		String sql = "select new com.bmp.model.vo.UserVoGroups(u.id,u.username,u.password,u.name,u.sex,u.tel,u.role,u.email,g.g_name) from User u,Groups g where u.g_id = g.id";
		list = session.createQuery(sql).setFirstResult((page-1)*rows).setMaxResults(rows).list();
		session.getTransaction().commit();
		//System.out.println("getUser");
		return list;
	}
	
}
